package com.dzk.cp.c11x5.index.zhx.cs;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.dzk.cp.c11x5.constant.C11x5Attrs;
import com.dzk.cp.c11x5.index.BaseC11x5Index;
import com.dzk.cp.c11x5.pojo.R5;
import com.dzk.cp.c11x5.util.C11x5Utils;
import com.dzk.cp.c11x5.util.SpringBeanUtils;
import com.dzk.cp.util.CpUtils;

public abstract class ZHXnCSIndex extends BaseC11x5Index {
	
	public ZHXnCSIndex setParams(int code, int num, int unit) {
		super.setParams(code, num, unit);
		return this;
	}
	
	@Override
	public Map<String, Integer> index() {
		Map<String, Integer> resultMap = new LinkedHashMap<String, Integer>();
		List<String> zhxcs = getAllZHXn();
		List<? extends R5> r5s = SpringBeanUtils.getR5Service(code).queryLatest(getLastestNum());
		for (String zhxc : zhxcs) {
			resultMap.put(zhxc, getCS(r5s, zhxc));
		}
		return resultMap;
	}

	public static Integer getCS(List<? extends R5> r5s, String zhxc) {
		int sx = 0;
		int[] zhxArr = CpUtils.split2Ints(zhxc);
		int n = (zhxc.length() + 1 ) / 3;
		for (R5 r5 : r5s) {
			int[] tmpArr = new int[] {r5.getN1(), r5.getN2(), r5.getN3(), r5.getN4(), r5.getN5()};
			if (C11x5Utils.compare(tmpArr, zhxArr, n)) {
				sx++;
			}
		}
		return sx;
	}
	
	protected abstract List<String> getAllZHXn();
	
	public static ZHXnCSIndex getInstance(int code, int num, int unit, int n) {
		switch (n) {
		case 1:
			return new ZHX1CSIndex().setParams(code, num, unit);
		case 2:
			return new ZHX2CSIndex().setParams(code, num, unit);
		case 3:
			return new ZHX3CSIndex().setParams(code, num, unit);
		case 4:
			return new ZHX4CSIndex().setParams(code, num, unit);
		case 5:
			return new ZHX5CSIndex().setParams(code, num, unit);
		default:
			return null;
		}
	}

}

class ZHX1CSIndex extends ZHXnCSIndex {

	@Override
	protected List<String> getAllZHXn() {
		return C11x5Attrs.getAllZHX1();
	}

	@Override
	protected int getZQ() {
		return 11;
	}

}

class ZHX2CSIndex extends ZHXnCSIndex {

	@Override
	protected List<String> getAllZHXn() {
		return C11x5Attrs.getAllZHX2();
	}

	@Override
	protected int getZQ() {
		return 110;
	}

}

class ZHX3CSIndex extends ZHXnCSIndex {

	@Override
	protected List<String> getAllZHXn() {
		return C11x5Attrs.getAllZHX3();
	}

	@Override
	protected int getZQ() {
		return 990;
	}

}

class ZHX4CSIndex extends ZHXnCSIndex {

	@Override
	protected List<String> getAllZHXn() {
//		return null;
		throw new RuntimeException("Unimplemented index!");
	}

	@Override
	protected int getZQ() {
		return 11 * 10 * 9 * 8;
	}

}

class ZHX5CSIndex extends ZHXnCSIndex {

	@Override
	protected List<String> getAllZHXn() {
//		return null;
		throw new RuntimeException("Unimplemented index!");
	}

	@Override
	protected int getZQ() {
		return 11 * 10 * 9 * 8 * 7;
	}

}
